package com.sunni.controller;

import com.sunni.entity.Adress;
import com.sunni.entity.User;
import com.sunni.entity.UserList;
import com.sunni.entity.UserMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

@RestController
@RequestMapping("/data")
public class data {

/*
2021.04.15*/

// 前台-->后台 参数改名；必传入参数；参数 默认值
    @RequestMapping("/required")
    public String required(@RequestParam(value = "num", required = false, defaultValue = "44") Integer id){
        return id+"";
    }

//  接收 数组
    @RequestMapping("/array")
    public String getArray(@RequestParam(value = "n",defaultValue = "null") String[] name){
        return Arrays.toString(name);
    }

//    接收 List集合
    @RequestMapping("/list")
    public String getList(UserList userList){
        StringBuffer str = new StringBuffer();
        for (User user : userList.getUserList()) {
            str.append(user+"\t");
        }
        return str.toString();
    }

/*
*2021.04.06
 *  */
//    接收 Map结合
    @RequestMapping("/map")
    public String getMap(UserMap userMap){
        StringBuffer str = new StringBuffer();
        for (Integer key : userMap.getUserMap().keySet()) {
            str.append(userMap.getUserMap().get(key));
        }
        return str.toString();
    }

//    接收 json数据，并返回json数据
    @RequestMapping("/json")
    public User getJson(@RequestBody User user){
        user.setName("局座");
        user.setAdress(new Adress(266011,"市北"));
        return user;
    }
}
